import {http} from '@/util/http.js'


const getBase64 = (file) =>{
	return new Promise((resolve, reject) => {
		const reader = new FileReader();
		reader.readAsDataURL(file);
		reader.onload = () => resolve(reader.result);
		reader.onerror = error => reject(error);
	});
}

const success = (msg = '成功！', duration = 1200) => {
	uni.showToast({
		title:msg,
		duration:duration,
		icon: 'success',
		fail() {
			console.log('消息提示失败-success')
		}
	})
}
const error = (msg = '失败！', duration = 1500) => {
	uni.showToast({
		title:msg,
		duration:duration,
		icon: 'none',
		fail() {
			console.log('消息提示失败-error')
		}
	})
}

const getTime10 = () =>{
	let tmp = Date.parse( new Date() ).toString();
	tmp = tmp.substr(0,10);
	return tmp*1;
}

const GetDistance = ( lat1,  lng1,  lat2,  lng2)=>{
    var radLat1 = lat1*Math.PI / 180.0;
    var radLat2 = lat2*Math.PI / 180.0;
    var a = radLat1 - radLat2;
    var  b = lng1*Math.PI / 180.0 - lng2*Math.PI / 180.0;
    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
    Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
    s = s *6378.137 ;// EARTH_RADIUS;
	s = Math.round(s * 10000) / 10000; // 调用 return的距离单位为km
    return s.toFixed(2); 
}

const getLocation = () => {
	return new Promise((resolve, reject) => {
		uni.getLocation({
			type: 'wgs84',
			success:async (res)=> {
				let resolve_data = {};
				// console.log('当前位置的经度：' + res.longitude);
				// console.log('当前位置的纬度：' + res.latitude);
				uni.setStorageSync('latitude',res.latitude)
				uni.setStorageSync('longitude',res.longitude)
				let location = await http('get','location/latlng_to_addr',{latitude:res.latitude,longitude:res.longitude})
				// console.log(222,location)
				if(location&&location.code==1){
					uni.setStorageSync('location',location)
					resolve_data=location
				}else{
					console.error('getLocation-失败：',location)
				}
				resolve(resolve_data)
			},
			fail:(e)=>{
				error('获取位置失败:'+e.errMsg,2000);
				reject(e)
			},
			complete:(e)=>{
				let configs = uni.getStorageSync('configs');
				if(configs&&configs.is_debug==1){
				    error(JSON.stringify(e));
				}
			},
		});
	})
}

const chooseLocation = () => {
	return new Promise((resolve, reject) => {
		uni.chooseLocation({
			success:  async (res) =>{
				let resolve_data = {};
				// console.log('选择的位置',res)
				let location = await http('get','location/latlng_to_addr',{latitude:res.latitude,longitude:res.longitude})
				if(location&&location.code==1){
					resolve_data=location
				}else{
					console.error('chooseLocation-失败：',location)
				}
				resolve(resolve_data)
			},
			fail() {
				error('选择位置失败:'+e.errMsg,2000);
				reject(e)
			}
		});
	})
}

const nav =  (url,delay=0)=> {
	setTimeout(()=>{
		uni.navigateTo({
			url,
			fail() {
				console.log('跳转失败-nav')
			}
		})
	},delay);
}
const redirect =  (url,delay=0)=> {
	setTimeout(()=>{
		uni.redirectTo({
			url:url,
			fail:(e)=>{
				console.error('跳转失败-redirect',e)
			}
		})
	},delay);
}
const back =  (url,delay=0)=> {
	setTimeout(()=>{
			uni.navigateBack({
				fail:(e)=>{
					console.error('跳转失败-back',e)
				}
			})
	},delay);
}
const copy =  (e) =>{
	uni.setClipboardData({
		data: e,
	});
}
const logout = (redirect=null)=> {
	uni.setStorageSync('token',null);
	uni.setStorageSync('user_id',null);
	uni.redirectTo({
		url:'/pages/index/index',
		fail:(e)=>{
			console.error('logout-fail',e);
		}
	});
}
const call_tel = (tel)=> {
	uni.makePhoneCall({
		phoneNumber: tel
	});
}

const formatTime = (time) => {
	if(!time)return '';
	// let date = new Date(time);
	let date = new Date(time * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
	let YYYY = date.getFullYear();
	let MM = date.getMonth() + 1;
	let DD = date.getDate();
	let hh = date.getHours();
	let mm = date.getMinutes();
	let ss = date.getSeconds();

	MM = MM < 10 ? ('0' + MM) : MM;
	DD = DD < 10 ? ('0' + DD) : DD;
	hh = hh < 10 ? ('0' + hh) : hh;
	mm = mm < 10 ? ('0' + mm) : mm;
	ss = ss < 10 ? ('0' + ss) : ss;

	return YYYY + '-' + MM + '-' + DD + ' ' + hh + ':' + mm + ':' + ss;
}

export default {
	getBase64,
	getTime10,
	success,
	error,
	getLocation,
	GetDistance,
	formatTime,
	redirect,
	nav,
	back,
	copy,
	logout,
	call_tel,
	chooseLocation,
}